package com.aidong.myprofile.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.activity.biz.MstCampaignBiz;
import com.aidong.dynamic.biz.TrnDynamicContentBiz;
import com.aidong.dynamic.biz.TrnPraiseBiz;
import com.aidong.myprofile.biz.MyAidongTimeBiz;
import com.aidong.myprofile.biz.MyPersonalMsgBiz;
import com.aidong.myprofile.entity.Appointment;
import com.aidong.util.PageUtil;
import com.aidong.util.ReadPropertiesUtil;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 我的爱动时刻
 * @author java
 * @version 1.0    
 *  2015.07.05
 */
@ParentPackage("json")
@Namespace("/")
public class MyAidongTimeAction implements ModelDriven<Appointment> {

	private Appointment appointment;
	
	@Override
	public Appointment getModel() {
		if(appointment == null){
			appointment = new Appointment();
		}
		return appointment;
	}
	
	private Map<String, Object> map;
	
	private int pageCurrent; // 页数
	
	private MyAidongTimeBiz aidongTimeBiz; // 爱动时刻service
	
	private TrnPraiseBiz trnPraiseBiz; // 点赞service
	
	private TrnDynamicContentBiz trnDynamicContentBiz;
	
	private MstCampaignBiz mstCampaignBiz;
	private MyPersonalMsgBiz personalMsgBiz; // 个人信息service
	
	@Autowired
	public void setTrnPraiseBiz(TrnPraiseBiz trnPraiseBiz) {
		this.trnPraiseBiz = trnPraiseBiz;
	}

	@Autowired
	public Appointment getAppointment() {
		return appointment;
	}
	
	@Autowired
	public void setMstCampaignBiz(MstCampaignBiz mstCampaignBiz) {
		this.mstCampaignBiz = mstCampaignBiz;
	}
	
	/**
	 * 个人信息service
	 * @param personalMsgBiz
	 */
	@Autowired
	public void setPersonalMsgBiz(MyPersonalMsgBiz personalMsgBiz) {
		this.personalMsgBiz = personalMsgBiz;
	}

	public void setAppointment(Appointment appointment) {
		this.appointment = appointment;
	}

	public TrnDynamicContentBiz getTrnDynamicContentBiz() {
		return trnDynamicContentBiz;
	}

	public void setTrnDynamicContentBiz(TrnDynamicContentBiz trnDynamicContentBiz) {
		this.trnDynamicContentBiz = trnDynamicContentBiz;
	}

	public int getPageCurrent() {
		return pageCurrent;
	}

	public MyAidongTimeBiz getAidongTimeBiz() {
		return aidongTimeBiz;
	}

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	public void setPageCurrent(int pageCurrent) {
		this.pageCurrent = pageCurrent;
	}

	/**
	 * 爱动时刻service
	 * @param aidongTimeBiz
	 */
	public void setAidongTimeBiz(MyAidongTimeBiz aidongTimeBiz) {
		this.aidongTimeBiz = aidongTimeBiz;
	}
	
	/**
	 * 我的爱动时刻
	 * @return
	 */
	@Action(value="getmyaidongtime",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMyAidongTime(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 爱动号
			if(idongId > 0){
				Map<String, Object> pageMap = PageUtil.page(10, pageCurrent); // 分页
				pageMap.put("idongId", idongId);
				List<Map<String, Object>> aidongTimeMap = aidongTimeBiz.getMyAidongTime(pageMap);
				for (Map<String, Object> time : aidongTimeMap) {
					time.put("picture", ReadPropertiesUtil.getIMGREAD(time.get("picture")));
				}
				
				// 朋友圈背景图
				Map<String, Object> memberMap = trnDynamicContentBiz.getDynamicBackImage(idongId);
				String backUrl = "";
				String headUrl = "";
				String nickName = "";
				if(memberMap != null){
					backUrl =ReadPropertiesUtil.getIMGREAD(memberMap.get("backUrl"));
					headUrl = ReadPropertiesUtil.getIMGREAD(memberMap.get("headUrl"));
					nickName = memberMap.get("nickName") + "";
				}
				
				Map<String, Object> bground = new HashMap<String, Object>();
				bground.put("backUrl", backUrl);
				bground.put("headUrl", headUrl);
				bground.put("nickName", nickName);
				
				map.put("code", 1);
				map.put("pageCurrent", pageMap.get("pageCurrent")); // 当前页
				map.put("result", aidongTimeMap);
				map.put("myCircle", bground);
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	/**
	 * 爱动时刻详情
	 * @return
	 */
	@Action(value="getmyaidongdetail",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getMyAidongTimeDetail(){
		map = new HashMap<String, Object>();
		try {
			int dynamicId =  appointment.getDynamicId(); // 动态ID
			int idongId = appointment.getIdongId(); // 爱动号
			if(dynamicId > 0){
				Map<String, Object> detailMap = aidongTimeBiz.getMyAidongTimeDetail(dynamicId);
				Map<String, Object> taskTags = personalMsgBiz.selectTaskTags(idongId);
				if(detailMap == null){
					map.put("code", 2);
					map.put("msg", "该动态已删除");
					return "succ";
				}else if(detailMap != null){
					detailMap.put("headUrl", ReadPropertiesUtil.getIMGREAD(detailMap.get("headUrl")));
					detailMap.put("picUrl", ReadPropertiesUtil.getIMGREAD(detailMap.get("picUrl")));
					detailMap.put("publishTime", getDay((Date)detailMap.get("publishTime")));
					detailMap.put("task1", Integer.parseInt(taskTags.get("task1").toString()));
					detailMap.put("task2", Integer.parseInt(taskTags.get("task2").toString()));
					detailMap.put("task3", Integer.parseInt(taskTags.get("task3").toString()));
				}
				
				// 点赞列表
				List<Map<String, Object>> praisesMap = aidongTimeBiz.getTrnPraiseByOne(dynamicId);
				if(praisesMap.size() > 0){
					for (Map<String, Object> praises : praisesMap) {
						String praHeadUrl = praises.get("headUrl") + "";
						if(!"".equals(praHeadUrl) && praHeadUrl != null){
							praises.put("headUrl", ReadPropertiesUtil.getIMGREAD(praHeadUrl));
						}else{
							praises.put("headUrl", "");
						}
					}
				}
				
				// 评论列表
				List<Map<String, Object>> commentsMap = aidongTimeBiz.getTrnDynamicCommentByOne(dynamicId);
				if(commentsMap.size() > 0){
					for (Map<String, Object> comments : commentsMap) {
						if(comments != null){
							String idongIdHeadUrl = comments.get("idongIdHeadUrl") + "";
							if(!"".equals(idongIdHeadUrl) && idongIdHeadUrl != null){
								comments.put("idongIdHeadUrl", ReadPropertiesUtil.getIMGREAD(idongIdHeadUrl));
							}
						}
						String recipientId = comments.get("recipientId") + "";
						String recipientIdNickName = comments.get("recipientIdNickName") + "";
						
						if(recipientId.equals("null")){
							comments.put("recipientId", "");
						}
						if(recipientIdNickName.equals("null")){
							comments.put("recipientIdNickName", "");
						}
						//增加判断评论多少天
						if(!"".equals(comments.get("commentTime")) && comments.get("commentTime")!=null){
							try{
								comments.put("commentTime", getDay((Date) comments.get("commentTime")));
							}catch(Exception e){
								e.printStackTrace();
								comments.put("commentTime", "");
							}
						}else{
							comments.put("commentTime", "");
						}
						
					}
				}
				
				// 查看当前登录用户是否对此条动态已点赞
				Map<String, Object> praiseParaMap = new HashMap<String, Object>();
				praiseParaMap.put("dynamicId", dynamicId);
				praiseParaMap.put("idongId", idongId);
				praiseParaMap.put("status", 1);
				int count = trnPraiseBiz.getCountPraise(praiseParaMap);
				if(idongId > 0){
					if(count > 0){ 
						// 已点赞
						detailMap.put("praiseStatus", 1);
					}else{
						// 未点赞
						detailMap.put("praiseStatus", 2);
					}
				}else{
					// 未点赞
					detailMap.put("praiseStatus", 2);
				}
				
				// 关注状态
				Map<String, Object> attionParaMap = new HashMap<String, Object>();
				attionParaMap.put("idongId", idongId); // 关注人
				attionParaMap.put("tionIdongId", detailMap.get("idongId")); // 被关注人
				int attionCount = mstCampaignBiz.getIfAttention(attionParaMap);
				if(idongId > 0){
					if(attionCount > 0){ // 已关注
						detailMap.put("attionStatus", 1);
					}else{ // 关注
						detailMap.put("attionStatus", 2);
					}
				}else{ // 关注
					detailMap.put("attionStatus", 2);
				}
				
				detailMap.put("praises", praisesMap);
				detailMap.put("comments", commentsMap);
				
				map.put("code", 1);
				map.put("result", detailMap);
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
	
	public static String getDay(Date begin){
		
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff =  new Date().getTime()-begin.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    if(day>0 && day<=7){
	    	return day+"天前";
	    }else if(day>7){
	    	SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd");
	    	return sm.format(begin);
	    }
	    
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    if(hour<1){
	    	return "刚刚";
	    }else/* if(hour>=1 && hour<=23)*/{
	    	return hour+"小时前";
	    }
	   /* // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    if(min>0){
	    	return min + "分钟前";
	    }else{
	    	return "1分钟前";
	    }*/
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    
	   // return day + "天" + hour + "小时" + min + "分钟";
	}
	
	public static String getDays(Date begin){
		long nd = 1000 * 24 * 60 * 60;
	    long nh = 1000 * 60 * 60;
	    long nm = 1000 * 60;
	    // long ns = 1000;
	    // 获得两个时间的毫秒时间差异
	    long diff =  new Date().getTime()-begin.getTime();
	    // 计算差多少天
	    long day = diff / nd;
	    if(day>0 && day<=2){
	    	return day+"天前";
	    }else if(day>2){
	    	SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd");
	    	return sm.format(begin);
	    }
	    
	    // 计算差多少小时
	    long hour = diff % nd / nh;
	    if(hour>0){
	    	return hour + "小时前";
	    }
	    // 计算差多少分钟
	    long min = diff % nd % nh / nm;
	    if(min>0){
	    	return min + "分钟前";
	    }else{
	    	return "1分钟前";
	    }
	    // 计算差多少秒//输出结果
	    // long sec = diff % nd % nh % nm / ns;
	    
	   // return day + "天" + hour + "小时" + min + "分钟";
	}
	
	/**
	 * 删除我的爱动时刻
	 * @return
	 */
	@Action(value="delaidongtime",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delAidongTime(){
		map = new HashMap<String, Object>();
		try {
			int idongId = appointment.getIdongId(); // 爱动号
			int dynamicId = appointment.getDynamicId(); // 动态ID
			if(idongId > 0 && dynamicId > 0){
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("idongId", idongId);
				paraMap.put("dynamicId", dynamicId);
				aidongTimeBiz.delAidongTime(paraMap);
				map.put("code", 1);
				map.put("msg", "删除成功");
				return "succ";
			}else{
				map.put("code", 0);
				map.put("msg", "参数缺失");
				return "succ";
			}
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", 0);
			map.put("msg", "系统异常");
			return "succ";
		}
	}
}